﻿Public Class MASACCEPTMCLASS
    Dim connect As CONDB = CONDB.NewConnection()

    Private ACCMID = Nothing
    Private ACCM_NAME = Nothing
    Private ACCM_NAME_EN = Nothing
    Private STATUS = Nothing

    Private selectMasacceptm = "SELECT accmid, accm_name, accm_name_en, `status` FROM masacceptm "

    Property selectMasacceptm_ As String
        Get
            Return selectMasacceptm
        End Get
        Set(value As String)
            selectMasacceptm = value
        End Set
    End Property

    Property ACCMID_ As Integer
        Get
            Return ACCMID
        End Get
        Set(value As Integer)
            ACCMID = value
        End Set
    End Property

    Property ACCM_NAME_ As String
        Get
            Return ACCM_NAME
        End Get
        Set(value As String)
            ACCM_NAME = value
        End Set
    End Property

    Property ACCM_NAME_EN_ As String
        Get
            Return ACCM_NAME_EN
        End Get
        Set(value As String)
            ACCM_NAME_EN = value
        End Set
    End Property

    Property STATUS_ As Boolean
        Get
            Return STATUS
        End Get
        Set(value As Boolean)
            If value = True Then
                STATUS = 1
            Else
                STATUS = 0
            End If
        End Set
    End Property

    Public Function insertMassacceptm() As Boolean
        If connect.GetTable("SELECT accmid FROM masacceptm WHERE accm_name = '" & ACCM_NAME & "' OR accm_name_en = '" & ACCM_NAME_EN & "'").Rows.Count > 0 Then
            MsgBox("ไม่สามารถเพิ่มรายการรับเงินดังกล่าวได้ เนื่องจากมีรายการดังกล่าวอยู่แล้ว")
            Return False
        Else
            Try
                connect.BeginTrans()

                Dim sql1 As String = ""
                Dim sql2 As String = ""
                Dim sql3 As String = ""

                If ACCMID IsNot Nothing Then
                    sql2 += "accmid, "
                    sql3 += "'" & ACCMID & "', "
                Else
                    sql2 += "accmid, "
                    sql3 += "default, "
                End If

                If ACCM_NAME IsNot Nothing Then
                    sql2 += "accm_name, "
                    sql3 += "'" & ACCM_NAME & "', "
                Else
                    sql2 += "accm_name, "
                    sql3 += "default, "
                End If

                If ACCM_NAME_EN IsNot Nothing Then
                    sql2 += "accm_name_en, "
                    sql3 += "'" & ACCM_NAME_EN & "', "
                Else
                    sql2 += "accm_name_en, "
                    sql3 += "default, "
                End If


                sql2 += "status "
                sql3 += "'" & STATUS & "' "

                sql1 += "INSERT INTO masacceptm (" & sql2 & ") VALUES (" & sql3 & ");"

                connect.ExecuteNonQuery(sql1)
                connect.CommitTrans()
                MsgBox("เพิ่มข้อมูลการรับเงินสำเร็จแล้ว")
                Return True
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
                Return False
            Finally
                connect.Dispose()
            End Try
        End If
    End Function

    Public Function editMasacceptm() As Boolean
        Try
            connect.BeginTrans()

            Dim sql As String = ""
            sql += "UPDATE masacceptm SET "

            sql += "accm_name = '" & ACCM_NAME & "', "
            sql += "accm_name_en = '" & ACCM_NAME_EN & "', "
            sql += "status = '" & STATUS & "' "

            sql += "WHERE accmid = '" & ACCMID & "'"

            connect.ExecuteNonQuery(sql)
            connect.CommitTrans()
            MsgBox("แก้ไขข้อมูลการรับเงินสำเร็จแล้ว")
            Return True
        Catch ex As Exception
            connect.RollbackTrans()
            MsgBox("ไม่สามารถแก้ไขข้อมูลได้" & ex.ToString)
            Return False
        Finally
            connect.Dispose()
        End Try
    End Function
End Class
